#include "mytest.h"
#include "ui_mytest.h"


mytest::mytest(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::mytest)
{
    ui->setupUi(this);

    qDebug() << "-----" << QThread::currentThread() << "----";
    mythread1* t1 = new mythread1();  //线程t1操作的是sqlite数据库
    mythread2* t2 = new mythread2();  //线程t2操作的是mysql数据库

    connect(this, &mytest::sendmysqladd, t2, &mythread2::recvAdd);
    connect(this, &mytest::sendstr, t1, &mythread1::recvStr);

    //是新增操作
    connect(ui->pushButton_3, &QPushButton::clicked, this, [=](){
        int id = ui->lineEdit->text().toInt();

        if(id == 0)
        {
             QMessageBox::critical(0, "失败", "用户id不能为空", QMessageBox::Ok);
             return;
        }

        QString name = ui->lineEdit_2->text();
        if(name == "")
        {
            QMessageBox::critical(this, "失败", "输入的姓名不能为空", QMessageBox::Ok);
            return;
        }

        QString passwd = ui->lineEdit_3->text();
        if(passwd == "")
        {
            QMessageBox::critical(this, "失败", "输入的密码不能为空", QMessageBox::Ok);
            return;
        }

        QString str = QString("insert into user values(%1, '%2', '%3')").arg(id).arg(name).arg(passwd);
        qDebug() << str;

        emit sendstr(str);
        t1->start();

        emit sendmysqladd(id);
        t2->start();
    });

    connect(t1, &mythread1::sendVector, this, [=](QVector<QString> vv){
            ui->tableWidget->clear();
            ui->tableWidget->setColumnCount(3);
            ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "用户id" << "姓名" << "密码");
            ui->tableWidget->setRowCount(vv.size() / 3 + vv.size() % 3);

            int k = 0;
            for(int i = 0; i < vv.size() / 3; i++)
            {
                for(int j = 0; j < 3; j++)
                {
                    ui->tableWidget->setItem(i, j, new QTableWidgetItem(vv[k++]));
                }
            }

    });




    connect(this, &mytest::sendselect, t1, &mythread1::recvSelect);
    connect(this, &mytest::sendmysqlselect, t2, &mythread2::recvSelect);
    //是查询操作
    connect(ui->pushButton_2, &QPushButton::clicked, this, [=](){
        emit sendselect();
        t1->start();

        emit sendmysqlselect(0);  //0 默认是全部查询
        t2->start();
    });



    connect(this, &mytest::SendMysqlAddSelect, t2, &mythread2::RecvAddSelect);
    connect(ui->pushButton_4, &QPushButton::clicked, [=](){
        emit SendMysqlAddSelect();
        t2->start();
    });


    connect(t2, &mythread2::SendVector, [=](QVector<QString> vv){
        ui->tableWidget->clear();
        ui->tableWidget->setColumnCount(4);
        ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "id" << "opr" << "happendtime" << "userid");
        ui->tableWidget->setRowCount(vv.size() / 4 + vv.size() % 4);

        int k = 0;
        for(int i = 0; i < vv.size() / 4; i++)
        {
            for(int j = 0; j < 4; j++)
            {
                ui->tableWidget->setItem(i, j, new QTableWidgetItem(vv[k++]));
            }
        }

    });
}



mytest::~mytest()
{
    delete ui;
}





